package cn.xawl.interview.thread;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author lh
 * @date 2020/08/24 19:05:37
 *
 * AtomicReference的使用
 **/
public class AtomicReferenceDemo {

    public static void main(String[] args) {
        AtomicReference<User> atomicReference = new AtomicReference<>();
        User zhangsan = new User("zhangsan", 12);
        User lisi = new User("lisi", 11);

        atomicReference.set(zhangsan);

        System.out.println(atomicReference.compareAndSet(zhangsan, lisi) + "\t" + atomicReference.get());
        System.out.println(atomicReference.compareAndSet(zhangsan, lisi) + "\t" + atomicReference.get());

    }


}


@Getter
@ToString
@AllArgsConstructor
class User {
    private String username;
    private Integer age;
}
